package com.bua.toothfactory;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import com.bua.datafactory.EquipmentDataFactory;
import com.bua.net.NetConfig;
import com.bua.net.RequestTask;
import com.bua.net.ResponseReport;
import com.bua.net.Tool;
import com.bua.toothfactory.config.Config;

import java.util.HashMap;

public class LauncherActivity extends Activity implements ResponseReport {

    private TextView tvDeviceID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        NetConfig.DeviceID = Tool.getMacAddress(this.getApplicationContext()).toUpperCase();
        String url = NetConfig.creatUrl(NetConfig.HttpRequestId.GET_DEPARTMENT);
        RequestTask request = new RequestTask();
        HashMap<String, String> requestData = NetConfig.GetEquipment(NetConfig.DeviceID);
        request.CreateTask(url, requestData, NetConfig.HttpRequestId.GET_DEPARTMENT.ordinal(), this);
        tvDeviceID =(TextView)findViewById(R.id.tvDeviceId);
        tvDeviceID.setText("设备编号：[ "+ NetConfig.DeviceID +" ]");
        tvDeviceID.setVisibility(View.INVISIBLE);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.launcher, menu);
        return true;
    }

    @Override
    public void callBack(String result, int requestId) {
        //To change body of implemented methods use File | Settings | File Templates.   \
        NetConfig.HttpRequestId RequestId = NetConfig.HttpRequestId.values()[requestId];
        switch (RequestId) {
            case GET_DEPARTMENT: {
                final EquipmentDataFactory obj = new EquipmentDataFactory(result);
                NetConfig.DepartmentID=obj.DepartmentId;
                Config.Hospital=obj.Hospital;
                Config.Department=obj.DepartmentName;
                if(obj.status==200)
                {
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        Intent intent = new Intent(LauncherActivity.this, OrderListActivity.class);
                        intent.putExtra("DepartmentId", obj.DepartmentId);
                        intent.putExtra("Hospital", obj.Hospital);
                        intent.putExtra("Department", obj.DepartmentName);
                        startActivity(intent);
                        LauncherActivity.this.finish();
                    }

                }, 2500);
                }
                else
                {
                    tvDeviceID.setVisibility(View.VISIBLE);

                }
            }
            break;

//            case REGISTER_EQUIPMENT: {
//                final EquipmentObject obj = new EquipmentObject(result);
//                new Handler().postDelayed(new Runnable() {
//
//                    @Override
//                    public void run() {
//                        Intent intent = new Intent(LauncherActivity.this, OrderListActivity.class);
//                        intent.putExtra("EquipmentId", obj.EquipmentId);
//                        startActivity(intent);
//                        LauncherActivity.this.finish();
//                    }
//
//                }, 2500);
//            }
//            break;
            default:
                return;
        }


    }
}
